# -*- coding: utf-8 -*-

import sys

import scrapy

from .. import items

reload(sys)
sys.setdefaultencoding('utf-8')


class CnblogsSpider(scrapy.Spider):
    name = 'cnblogs'  # 爬虫的名称
    allowed_domains = ['cnblog.com']  # 允许的域名
    start_urls = ['http://www.cnblogs.com/qiyeboy/default.html?page=1']

    def parse(self, response):
        pagers = scrapy.Selector(response).xpath(".//*[@class='day']")
        for pager in pagers:
            url = pager.xpath(".//*[@class='postTitle']/a/@href").extract()[0]
            title = pager.xpath(".//*[@class='postTitle']/a/text()").extract()[0]
            time = pager.xpath(".//*[@class='dayTitle']/a/text()").extract()[0]
            content = pager.xpath(".//*[@class='postCon']/div/text()").extract()[0]
            item = items.CnblogspiderItem(url=url, title=time, time=title.encode('utf-8'),
                                          content=content.encode('utf-8'))
            request = scrapy.Request(url=url, callback=self.parse_body)
            request.meta['item'] = item  # 将item暂存
            yield request
        next_page = scrapy.Selector(response).re(u'<a href="(\S*)">下一页</a>')
        if next_page:
            yield scrapy.Request(url=next_page[0], callback=self.parse_body)

    def parse_body(self, response):
        item = response.meta['item']
        body = response.xpath(".//*[@class='postBody']")
        item['cimage_urls'] = body.xpath('.//img//@src').extract()  # 提取图片链接

        print item['cimage_urls']
        yield item
